package tcln.sors.model.core.util.dataloader;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.DateUtils;

import tcln.sors.model.contact.dom.Contact;
import tcln.sors.model.order.dom.Order;
import tcln.sors.model.order.dom.OrderItem;
import tcln.sors.model.order.dom.OrderItemHist;
import tcln.sors.model.order.dom.OrderStatus;
import tcln.sors.model.product.dom.Product;

/**
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class SampleOrderDataLoadingCommand extends DataLoadingCommand {

    /**
     * {@inheritDoc}
     */
    @Override
    protected void doExecute(Object input, Object[] additionalParams) {
        List<Product> products = getDaoFactory().getDaoFor(Product.class).getAll();
        List<Contact> contacts = getDaoFactory().getDaoFor(Contact.class).getAll();

        int productCount = products.size();
        for (int i = 0; i < productCount / 2; i++) {
            int randomIndex = RandomUtils.nextInt(productCount);
            Product product = products.get(randomIndex);
            
            for (int k = 0; k < contacts.size(); k++) {
                Contact contact = contacts.get(k);
                int periodInDays = 3;

                // create order - PREPARING
                Order order = new Order();
                order.setCreationDate(new Date());
                order.setCustomer(contact);
                order.setExpiryDate(DateUtils.addDays(new Date(), periodInDays));
                order.setName("Test order");
                order.setRemindBefore(2);
                Integer newOrderId = getDaoFactory().getDaoFor(Order.class).saveOrUpdate(order).getId();
                
                // process all orders except for the last one
                if (k < contacts.size() - 1) {
                    OrderItem item = new OrderItem();
                    item.setCreationDate(order.getCreationDate());
                    item.setDeliveryDate(order.getDeliveryDate());
                    item.setExpiryDate(order.getExpiryDate());
                    item.setHeld(true);
                    item.setOrder(order);
                    item.setProduct(product);
                    item.setQuantity(12);
                    item.setRemindBefore(1);
                    getDaoFactory().getDaoFor(OrderItem.class).saveOrUpdate(item);
                    
                    for (int j = 1; j <= periodInDays; j++) {
                        OrderItemHist itemHist = new OrderItemHist();
                        itemHist.setDeliveryDate(DateUtils.addDays(new Date(), j));
                        itemHist.setOrderItem(item);
                        itemHist.setQuantity(item.getQuantity() / periodInDays);
                        getDaoFactory().getDaoFor(OrderItemHist.class).saveOrUpdate(itemHist);
                    }
                    
                    Order currentOrder = getDaoFactory().getDaoFor(Order.class).findById(newOrderId);
                    currentOrder.setStatus(OrderStatus.CLOSED);
                }
            }
        }
    }
}
